/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package com.godenwater.recv.server.all;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.SocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.gov.mwr.sl651.HydroBuilder;
import cn.gov.mwr.sl651.IMessage;
import cn.gov.mwr.sl651.IMessageBody;
import cn.gov.mwr.sl651.IMessageHeader;
import cn.gov.mwr.sl651.Symbol;
import cn.gov.mwr.sl651.body.DataItem;
import cn.gov.mwr.sl651.body.Up36Body;
import cn.gov.mwr.sl651.command.UpCommand;
import cn.gov.mwr.sl651.parser.HexParser;
import cn.gov.mwr.sl651.utils.ByteUtil;
import cn.gov.mwr.sl651.utils.StcdParser;

import com.godenwater.yanyu.YYBuilder;
import com.godenwater.recv.model.CommonMessage;

/**
 * An UDP client taht just send thousands of small messages to a UdpServer.
 * 
 * This class is used for performance test purposes. It does nothing at all, but
 * send a message repetitly to a server.
 * 
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 */
public class RtuClient extends IoHandlerAdapter implements Runnable {

	private Logger logger = LoggerFactory.getLogger(RtuClient.class);

	/** The connector */
	private IoConnector connector;

	/** The session */
	private static IoSession session;

	private boolean received = false;

	private UpCommand cmd = new UpCommand();

	private byte upDown = Symbol.UP;

	private int mPort = 5017;

	private RtuClient() {

	}

	/**
	 * Create the UdpClient's instance
	 */
	public RtuClient(int _port) {
		connector = new NioSocketConnector();
		connector.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new RtuCodecFactory(false,"comm")));
		connector.setHandler(this);

		SocketSessionConfig dcfg = (SocketSessionConfig) connector
				.getSessionConfig();
		dcfg.setWriteTimeout(30);

		mPort = _port;
		// 创建连接
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "localhost", 50170));// 101.200.195.117
		// 海鹏
//		 ConnectFuture connFuture = connector.connect(new InetSocketAddress(
//		 "101.200.195.117", 5017));// 50170, 20011
		//
		ConnectFuture connFuture = connector.connect(new InetSocketAddress(
				"127.0.0.1", 5017));//5800,6688,5888

//		ConnectFuture connFuture = connector.connect(new InetSocketAddress(
//				"218.200.69.24", 5010));//5800,6688,5888
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "101.200.195.117", mPort));//5017
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "172.21.109.2", 20011));// 20011
		// 赤峰
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "124.67.253.57", 7000));
		// 呼伦贝尔
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "58.18.176.141", 7000));
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "61.138.124.206", 7000));
		// 通辽
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "116.116.181.138", 7000));
		// 锡林郭勒
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "58.18.112.54", 7000));
		// 呼和浩特
		// ConnectFuture connFuture = connector.connect(new InetSocketAddress(
		// "116.113.106.190", 7000));
		// 等待是否连接成功，相当于是转异步执行为同步执行。
		connFuture.awaitUninterruptibly();

		// 获取session
		session = connFuture.getSession();

		cmd.setCenterAddr(ByteUtil.HexStringToBinary("FF"));
		cmd.setStationAddr(ByteUtil.HexStringToBinary("0011223344"));
		cmd.setPassword(ByteUtil.HexStringToBinary("FFFF"));
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		// cause.printStackTrace();
		logger.info(">> Client 会话出现异常  " + session.getRemoteAddress() + " \t"
				+ cause.getMessage(), cause);
	}

	public void viewInfo(HexParser parser, IMessage message) throws Exception {

		IMessageHeader header = message.getHeader();
		IMessageBody body = parser.parse36Body(message.getBody().getContent());

		Up36Body body36 = (Up36Body) body;

		System.out.println("******* 中心站查询遥测站实时数据 *******");
		System.out.println("原始包长度：" + header.getBodySize() + "字节");
		// System.out
		// .println("原始包信息：" + );
		System.out.println("中心站地址："
				+ ByteUtil.toHexString(header.getCenterAddr()));
		System.out.println("水文特征码：00，水文测站编码："
				+ StcdParser.parseStcd(header.getStationAddr()));
		System.out.println("密码：" + ByteUtil.toHexString(header.getPassword()));
		System.out.println("功能码：" + ByteUtil.toHexString(header.getFuncCode()));
		System.out.println("流水号：" + body36.getSerialId() + "，发报时间："
				+ body36.getSendDate());

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		received = true;

		// 纯粹只处理报文，此消息体已经被解码为单体报文
		CommonMessage msg = (CommonMessage) message;
		Object msgheader = msg.getHeader();
		if (msgheader instanceof cn.gov.mwr.sl651.header.HexHeader) {

			cn.gov.mwr.sl651.IMessageHeader header = (cn.gov.mwr.sl651.IMessageHeader) msg
					.getHeader();
			String hHeader = HydroBuilder.toHexString(header,
					cn.gov.mwr.sl651.Symbol.DOWN);
			String hBody = ByteUtil.toHexString(msg.getContent());
			String hEof = ByteUtil.toHexString(msg.getEOF());
			String hCrc = ByteUtil.toHexString(msg.getCRC());
			System.out.println(hHeader + hBody + hEof + hCrc);

		} else if (msgheader instanceof cn.gov.mwr.sl651.header.AscHeader) {

			cn.gov.mwr.sl651.IMessageHeader header = (cn.gov.mwr.sl651.IMessageHeader) msg
					.getHeader();

			String hHeader = HydroBuilder.toHexString(header,
					cn.gov.mwr.sl651.Symbol.DOWN);
			String hBody = ByteUtil.toHexString(msg.getContent());
			String hEof = ByteUtil.toHexString(msg.getEOF());
			String hCrc = ByteUtil.toHexString(msg.getCRC());
			System.out.println(hHeader + hBody + hEof + hCrc);

		} else if (msgheader instanceof com.godenwater.yanyu.YYMessageHeader) {

			com.godenwater.yanyu.IMessageHeader header = (com.godenwater.yanyu.IMessageHeader) msg
					.getHeader();

			String hHeader = YYBuilder.toHexString(header,
					cn.gov.mwr.sl651.Symbol.DOWN);
			String hBody = ByteUtil.toHexString(msg.getContent());
			String hEof = ByteUtil.toHexString(msg.getEOF());
			String hCrc = ByteUtil.toHexString(msg.getCRC());
			System.out.println(hHeader + hBody + hEof + hCrc);

		} else {
			// 未知的报文

		}

		// IParser parser = new HexParser();
		// // viewInfo(new HexParser(),msg);
		// byte eof = msg.getEOF();
		// if (eof == Symbol.EOT) {
		// System.out.println(">> ST "
		// + parser.parseStcd(header.getStationAddr()) + " 确认报文。");
		//
		// } else if (eof == Symbol.ESC) {
		// System.out.println(">> ST "
		// + parser.parseStcd(header.getStationAddr())
		// + " 确认报文，需保持在线！");
		//
		// } else if (eof == Symbol.ESC) {
		// System.out.println(">> ST "
		// + parser.parseStcd(header.getStationAddr())
		// + " 查询报文，需保持在线！");
		// }

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void sessionClosed(IoSession session) throws Exception {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void sessionCreated(IoSession session) throws Exception {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void sessionOpened(IoSession session) throws Exception {
	}

	public void run() {
		int i = 1;
		try {
			// Date curTime = new Date();
			// DateUtil.addSeconds(curTime, 20);

			/*
			 * 此处测试无问题 String dataFile = "d:/图片报文数据.msg";
			 * 
			 * List<String> lines = FileUtils.readLines(new File(dataFile),
			 * "UTF-8");
			 * 
			 * for (String line : lines) {
			 * 
			 * line = StringUtils.replace(line, " ", ""); if
			 * (StringUtils.isNotBlank(line)) { System.out.println(">> line " +
			 * i + " " + line); sendMessage(line);
			 * 
			 * Thread.sleep(100); i++; } }*
			 */

			// 燕禹固态报文，此处测试无问题
			// String dataFile = "d:/新规约固态报文多行.txt";
			//
			// List<String> lines;
			// try {
			// lines = FileUtils.readLines(new File(dataFile), "UTF-8");
			//
			// for (String line : lines) {
			//
			// line = StringUtils.replace(line, " ", "");
			// if (StringUtils.isNotBlank(line)) {
			// System.out.println(">> line " + i + " " + line);
			// sendMessage(line);
			//
			// Thread.sleep(100);
			// i++;
			// }
			// }
			//
			// } catch (IOException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }

			String line = "";
//			// 时差法测流报文
// 			line = "7E 7E 01 00 12 34 56 78 12 34 32 00 26 02 00 01 16 11 14 16 40 00 F1 F1 00 12 34 56 78 48 F0 F0 16 11 14 16 40 39 1A 00 27 30 37 23 FF 00 11 23 38 12 12 20 03 E6 61";
//			sendMessage(StringUtils.replace(line, " ", ""));
			 //"7E7E30556677889900003800DE16002001000A121116164136F1F1556677889950F0F012111600000418000000F4F400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001E000000000000000000000000000000000000000014000000000000000000173E1A";
			 //sendMessage(line);

			// Thread.sleep(100);

			// line =
			// "7E7E305566778899000038006216002002000000000000000F19000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000038DDA";
			// sendMessage(line);

			// line =
			// "7E 7E 00 12 34 56 78 10 12 34 38 80 17 02 00 00 13 03 25 10 59 22 13 03 25 10 13 03 25 11 04 18 00 00 00 F4 60 05 42 BE";

			// line =
			// "7E 7E 01 00 00 00 00 06 12 34 36 01 EF 16 02 20 01 00 02 15 07 25 20 00 34 F1 F1 00 00 00 00 06 48 F0 F0 15 07 25 20 00 F3 01 FF D8 FF E0 00 10 4A 46 49 46 00 01 01 00 00 01 00 01 00 00 FF DB 00 43 00 12 0C 0C 0F 0C 0C 12 0F 0F 0F 12 12 12 15 18 2A 1B 18 18 18 18 33 24 27 1E 2A 3C 33 3F 3C 39 33 39 39 42 48 5D 4E 42 45 57 45 39 39 51 6F 51 57 60 63 69 69 69 3F 4E 72 7B 72 66 78 5D 66 69 63 FF DB 00 43 01 12 12 12 18 15 18 30 1B 1B 30 63 42 39 42 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 FF C4 00 1F 00 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0A 0B FF C4 00 B5 10 00 02 01 03 03 02 04 03 05 05 04 04 00 00 01 7D 01 02 03 00 04 11 05 12 21 31 41 06 13 51 61 07 22 71 14 32 81 91 A1 08 23 42 B1 C1 15 52 D1 F0 24 33 62 72 82 09 0A 16 17 18 19 1A 25 26 27 28 29 2A 34 35 36 37 38 39 3A 43 44 45 46 47 48 49 4A 53 54 55 56 57 58 59 5A 63 64 65 66 67 68 69 6A 73 74 75 76 77 78 79 7A 83 84 85 86 87 88 89 8A 92 93 94 95 96 97 98 99 9A A2 A3 A4 A5 A6 A7 A8 A9 AA B2 B3 B4 B5 B6 B7 B8 B9 BA C2 C3 C4 C5 C6 C7 C8 C9 CA D2 D3 D4 D5 D6 D7 D8 D9 DA E1 E2 E3 E4 E5 E6 E7 E8 E9 EA F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FF C4 00 1F 01 00 03 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0A 0B FF C4 00 B5 11 00 02 01 02 04 04 03 04 07 05 04 04 00 01 02 77 00 01 02 03 11 04 05 21 31 06 12 41 51 07 61 71 13 22 32 81 08 14 42 91 A1 B1 C1 09 23 33 52 F0 15 62 72 D1 0A 16 24 17 A9 82";
			// line = StringUtils.replace(line, " ", "");
			// sendMessage(line);

			// 提取固态水位数据
			// line =
			// "7E 7E 16 00 12 34 56 78 12 34 38 00 66 02 0D BE 16 10 28 15 00 51 F1 F1 00 12 34 56 78 48 F0 F0 16 10 28 08 00 04 18 00 00 00 F5 C0 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 93 0E 92 0E 93 0E 91 0E 95 0E 8D 0E 9D 0E 7D 0E BD 0E 3D 0F 3D 0D 3D 11 3D 03 74 1B 7E 7E 16 00 12 34 56 78 12 34 32 00 64 02 0D BF 16 10 28 15 05 02 F1 F1 00 12 34 56 78 48 F0 F0 16 10 28 15 05 F4 60 00 00 00 00 00 00 00 00 00 00 00 00 F5 C0 11 3D FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 26 19 00 03 40 20 19 00 01 50 1A 19 00 00 00 39 1A 00 44 13 37 23 00 00 16 31 FF 21 22 00 00 00 90 38 12 12 52 03";
			// line = StringUtils.replace(line, " ", "");
			// sendMessage(line);

			// line =
			// "7E 7E 16 00 12 34 56 78 12 34 38 00 42 02 0D BC 16 10 28 14 55 50 F1 F1 00 12 34 56 78 48 F0 F0 16 10 28 08 00 04 18 00 00 00 F4 60 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0A 05 00 0A 14 05 05 19 05 05 14 0A 03 9C AF";
			// line = StringUtils.replace(line, " ", "");
			// sendMessage(line);

			// while (true) {
			// sendCommand("34");//
			// sendCommand("38");38号文是多报文上发的，还需要再加强测试，接收端处理还没有解决
			// sendYY06Message();
			i++;
			Thread.sleep(100);
			// }

			//发送燕禹报文
			//sendYY06Message();
			
			//发送升级报文
			//line = "7E 7E 01 00 12 34 56 78 12 34 32 00 26 02 00 01 16 11 14 16 40 00 F1 F1 00 12 34 56 78 48 F0 F0 16 11 14 16 40 39 1A 00 27 30 37 23 FF 00 11 23 38 12 12 20 03 E6 61";
 			//send34Message();
			//人工置数报
			line = "7E 7E 10 00 12 34 56 78 12 34 35 00 21 02 00 0F 13 03 25 09 58 04 f2 F2 79 61 6E 79 75 31 39 38 38 3A 59 43 5A 2D 32 41 2D 31 30 31 21 21 21 03 B6 98";

			//燕禹报文
			line = "7E109B0E3F1701161400065500000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1380FFFFFFFFFFFFFFFFFFFF00035B6F";

			line = "7E10570E3F170117100005EE0000000000000000000000000000000001D601D601D601D601D601D601D601D601D601D601D601D51380FFFFFFFFFFFFFFFFFFFF00170853";

			//line="7B01001631333535343539303639340AA6D62F0FA27B";

			//宏电报文
			//line = "7B01001631333931323334353637380AAB570D0FA27B";//
			//sendMessage(StringUtils.replace(line, " ", ""));

			Thread.sleep(500);
			line = "7B09001031333931323334353637387B7E11FF0E3F1506241100000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1210FFFFFFFFFFFFFFFFFFFF00032DCD";
			//line = "7E 13 78 0E A0 13 10 29 16 00 00 0E 00 00 00 0E 00 00 00 00 00 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 12 10 00 00 00 00 00 00 00 00 00 00 01 20 20 20 31 2E 36 35 33 20 20 20 31 2E 37 34 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 20 20 20 31 2E 36 35 33 00 17 09 BD";
			//line = "7E 63 05 09 57 12 08 08 07 51 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 12 30 FF FF FF FF 00 10 FF FF FF FF 11 03 F6 12 04 5A 13 03 F6 14 03 F6 15 03 F6 16 03 F6 FF FF FF FF FF FF 00 17 2C 75";
			line="7e112d0e9f17031116000000000000000000000000000000000000000aaa0aaa0aaa0aaa0aaa0aaa0aaa0aaa0aaa0aaa0aaa0aaa1360ffffffffffffffffffff202020302e363835202020302e363835202020312e363831202020312e373637202020312e373637202020312e373637202020312e373637202020312e373637202020312e373637202020312e363336202020312e363336202020312e36333600032a2d";

			line="7E7E01001234567800014A000F020001170720102600F1F10012345678032B38" ;
			//sendMessage(StringUtils.replace(line, " ", ""));
			send36Message();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	public void stop() {
		if (connector.isActive()) {
			connector.dispose(true);
		}
	}

	protected void sendCommand(String command) {
		cmd.setBodyStartBit(Symbol.STX);

		IMessage message = null;
		// ------------------------------
		if (StringUtils.isNotEmpty(command)) {

			if (command.length() > 2) {
				// 直接发送命令
				session.write(ByteUtil.HexStringToBinary(command));
				return;
			}

			command = StringUtils.trim(command).toUpperCase();

			byte[] funcCode = ByteUtil.HexStringToBinary(command);
			switch (funcCode[0]) {
			case 0x06: // 链路报
				sendYY06Message();
				break;
			case 0x0A: // 链路报
				sendYY0AMessage();
				break;

			case 0x2F: // 链路报
				message = send2FMessage();
				break;
			case 0x30: // 测试报
			case 0x32: // 定时报
				send32Message();
				break;
			case 0x34: // 小时报
				send34Message();
				break;
			case 0x33: // 加报报
				send33Message();
				break;
			case 0x31: // 均匀时段报
				send31Message();
				break;
			case 0x35: // 人工置数报
				message = send35Message();
				break;
			case 0x36: // 图片报
				send36Message();
				break;
			case 0x37: //
				send37Message();
				break;
			case 0x38: //
				send38Message();
				break;
			case 0x39: //
				send39Message();
				break;
			case 0x3A: //
				send3AMessage();
				break;
			case 0x40: //
				send40Message();
				break;
			case 0x41: //
				send41Message();
				break;
			case 0x42: //
				send42Message();
				break;
			case 0x43: //
				send43Message();
				break;
			case 0x44: //
				send44Message();
				break;
			case 0x45: //
				send45Message();
				break;
			case 0x46: //
				send46Message();
				break;
			case 0x47: //
				send47Message();
				break;
			case 0x48: //
				send48Message();
				break;
			case 0x49: //
				send49Message();
				break;
			case 0x4A: //
				send4AMessage();
				break;
			case 0x4B: //
				send4BMessage();
				break;
			case 0x4C: //
				send4CMessage();
				break;
			case 0x4D: //
				send4DMessage();
				break;
			case 0x4E: //
				send4EMessage();
				break;
			case 0x4F: //
				send4FMessage();
				break;
			case 0x50: //
				send50Message();
				break;
			case 0x51: //
				send51Message();
				break;
			case 0x62: //
				send32Message();
				break;
			case (byte) 0xC0: // 水資源數據報
				sendC0Message();
				break;
			default:
				System.out.println("未知指令，请重新发送正确指令或输入HELP进行指令查看.");
			}

			if (message != null) {
				session.write(message);
				System.out.println(this + "-----遥测站发送报文-----功能码：0x" + command
						+ "-----");
				cmd.printHexString(message);
				System.out.println("-----------------------------------------");
			}
		}
	}

	public void sleep(int seconds) {
		try {
			Thread.sleep(seconds);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void sendYY06Message() {

		String hexString = "7E11FF067B0001AAAAAA1510291417FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF176622";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);

		sleep(100);

		hexString = "7E11FF067B0002FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1789A5";
		byte[] message2 = ByteUtil.HexStringToBinary(hexString);
		session.write(message2);

		sleep(100);

		hexString = "7E11FF067B0003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF178564";
		byte[] message3 = ByteUtil.HexStringToBinary(hexString);
		session.write(message3);

		sleep(100);

		hexString = "7E11FF067B000403F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F817D788";
		byte[] message4 = ByteUtil.HexStringToBinary(hexString);
		session.write(message4);

		sleep(100);

		hexString = "7E11FF067B000503F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F803F817DB49";
		byte[] message5 = ByteUtil.HexStringToBinary(hexString);
		session.write(message5);

	}

	public void sendYY0AMessage() {

		String hexString = "7E10020E3F150704090000290000900000000000000000000000000069696969696969696969696969696969696969696969696912200000000014000000FFFF0003C953";

		hexString = "7e100c0a7b0001dddddd1502122206220622062206220622062206220622062206220622062219221922202220222022202224222422242224135313531353100210031003104010401040104210421054105410541054120412041204120412041204120412041204120412041204120412041204120412241224122417ec39";

		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);

		// hexString =
		// "7E11FF067B0002FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1789A5";
		// byte[] message2 = ByteUtil.HexStringToBinary(hexString);
		// session.write(message2);

	}

	/**
	 * 6.6.4.2　链路维持报
	 */
	public IMessage send2FMessage() {

		cmd.setFuncCode(new byte[] { (byte) 0x2F });
		cmd.setBodyStartBit(Symbol.STX);
		cmd.setEof(Symbol.ETX);
		return cmd.send2FMessage(1, null);
	}

	/**
	 * 6.6.4.3　测试报
	 */
	public IMessage send30Message() {
		cmd.setBodyStartBit(Symbol.STX);
		cmd.setEof(Symbol.ETX);

		List<DataItem> items = new ArrayList();
		DataItem item = new DataItem();

		// 20H //当前降水量PJ
		String value = "123.45";
		int decimal = HydroBuilder.buildDecimal(value);
		value = StringUtils.replace(value, ".", "");
		byte[] pjData = ByteUtil.str2Bcd(value);
		byte datalen = HydroBuilder.buildDataLength(pjData.length, decimal);

		item.setLabel(new byte[] { (byte) 0x20, datalen });
		item.setValue(pjData);
		items.add(item);

		// 38H //电压// VT
		value = "12.00";
		value = StringUtils.replace(value, ".", "");
		byte[] vtData = ByteUtil.str2Bcd(value);
		byte vtDataLen = HydroBuilder.buildDataLength(vtData.length, decimal);
		DataItem item2 = new DataItem();
		item2.setLabel(new byte[] { (byte) 0x38, vtDataLen });
		item2.setValue(vtData);
		items.add(item2);

		String stcd = "0011110000";
		String sttp = "P";

		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
		String viewDate = sdf.format(new Date(System.currentTimeMillis()));

		return cmd.send30Message(1, null, stcd, sttp, viewDate, items);

	}

	/**
	 * 人工置数报，测试通过
	 */
	public void send31Message() {

		// String hexString =
		// "7E7E010040548583000A310036020008140508120018F1F1000000000148F0F014050811050418000005F5C0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF035AFE";

		String hexString = "7E7E010020321200000A31002A029999140531150001F1F1002032120048F0F014053114050418000005F4600000000000000000000000000326C32C";
		hexString = "7E7E010020345600000A310036020031120510090018F1F1002034560048F0F012051008050418000005F5C0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF033D7A";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send32Message() {
		String hexstr = "7E7EFF0000000003FFFF32005E020024130503160000F1F1000000000348F0F01305031500F460000000000000000000000000F5C0000B000B000B000B000B000BFFFF000B000B000B000B000BF0F0130503160020190000001F190000002619000030391A0000123812126003B98B";
		hexstr = "7E7E020040544500303932002502822CA20826150200F1F1004054450050F0F0A208261500201900003826190547463812119403E124";
		hexstr = "7E7E010011420410000A320025020023140917110018F1F1001142041048F0F0140917110020190011002619002522381213390370C4";
		hexstr = "7E7E010020152800000A320041020035140829170002F1F1002015280048F0F014082917002019000000F0F01408291605F460000000000000000000000000F0F01408291700261900294038121315035A2A";
		hexstr = "7E7E010020346800000A320041020035140829170002F1F1002034680048F0F014082917002019000002F0F01408291605F460000000000200000000000000F0F014082917002619002898381213040397EA";
		hexstr = "7E7E010090000006000A320047020003150326160004F1F1009000000648F0F015032616002019000412F0F01503261505F46000FFFFFFFF1E005400000200F0F0150326160026190005243923FFFFFFFF38121171033497";
		hexstr = "7E7E010090000006000A310042020002150326230001F1F1009000000648F0F0150326220504180000052219FFFFFFFFFFFF00000000003000000000000000000000000000000000000000000000000003753B";
		hexstr = "7E7E010090000006000A320041020003150326230003F1F1009000000648F0F015032623002019000030F0F01503262205F460FFFF001E0000000000000000F0F0150326230026190000303812117103121E";
		hexstr = "7E7EFF0001724600E240340035020012150327100002F1F1000172460048F0F01503270905F460000000000000000000000000F0F0150327100026190000003812141103E837";
		hexstr = "7E7EFF0001722400E240340035020161150327090003F1F1000172240048F0F01503270805F460121E0E0A0A060E161406141AF0F01503270900261902647838121367031399";
		hexstr = "7E7E010090000007000A31004202000A150327140001F1F1009000000748F0F0150327130504180000052219000000000000000000000000000000000000000000000000000000000000000000000000030BCA";
		hexstr = "7E7E010090000001000A32004102000B150327140002F1F1009000000148F0F015032714002019000100F0F01503271305F460000000000000000000000000F0F01503271400261900013038121171035012";
		hexstr = "7E7E010090000001000A320047020007150329120008F1F1009000000148F0F015032912002019000000F0F01503291105F460000000000000000000000000F0F0150329120026190002283923FFFFFFFF3812117103512B";
		byte[] message = ByteUtil.HexStringToBinary(hexstr);
		session.write(message);

	}

	public void send33Message() {
		// String hexstr =
		// "01464630303132333435363738303030413333303034310230303034313330333037303631383330535420303031323334353637382048205454203133303330373036313820505220303030322E302056542031322E3333200335383337";
		String hexstr = "013432303031313232333334343034443233333030343102303032423133303330383130313835315354203030313132323333343420482054542031333033303831303138205A20303031322E39392056542031322E3332200332453939";
		// 雨量加报
		hexstr = "7E7E3C8888666601000033001C020005130104134800F1F1888866660150F0F01301041348261900003403A777";
		// 水位加报
		hexstr = "7E7E3C8888666601000033001D020006130104135201F1F1888866660150F0F0130104135239230002409003BAFF";
		byte[] message = ByteUtil.HexStringToBinary(hexstr);
		session.write(message);

	}

	/**
	 * 小时报
	 */
	public void sendMessage(String hexString) {

		hexString = StringUtils.replace(hexString, " ", "");
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	/**
	 * 小时报
	 */
	public void send34Message() {

		// String hexString =
		// "7E7E010040548583000A310036020008140508120018F1F1000000000148F0F014050811050418000005F5C0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF035AFE";

		String hexString = "7E7E010020321200000A31002A029999140531150001F1F1002032120048F0F014053114050418000005F4600000000000000000000000000326C32C";
		hexString = "7E7E010020345600000A310036020031120510090018F1F1002034560048F0F012051008050418000005F5C0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF033D7A";
		hexString = "7E7E3C88886666010000340053020008130104140000F1F1888866660150F0F01301041400F4F400000000000000000A180000F5F5FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF05A50D2D09690B4B261900003420190000343923000289103812121003AB6C";
		//hexString = "7E 7E AA AA AA AA AA AA 12 34 33 00 57 02 07 CC 16 09 05 15 10 02 F1 F1 AA AA AA AA AA 48 F0 F0 16 09 05 15 10 F4 60 00 32 00 00 00 00 00 00 00 00 00 00 F5 C0 0A FD 0A FD FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 26 19 00 00 50 20 19 00 00 50 1A 19 00 00 50 39 1A 00 28 13 38 12 11 40 03 7E 9B";
		hexString = StringUtils.replace(hexString, " ", "");
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	/**
	 * 人工置数报，测试通过
	 */
	public IMessage send35Message() {
		cmd.setFuncCode(new byte[] { (byte) 0x35 });
		cmd.setBodyStartBit(Symbol.STX);
		cmd.setEof(Symbol.ETX);
		String rgzs = "P 11110000 09090900 P1 10.9 WS 8 TT 09011000 P1 19 WS 8 TT 09011100 P1 19 WS 8 TT 09012000 P1 19 WS 8 TT 09013000 P1 19 WS 8 TT 09014000 P1 19 WS 8 TT 09015000 P1 19 WS 8 TT 09016000 P1 19 WS 8 TT 09017000 P1 19 WS 8 TT 09018000 P1 19 WS 8 TT 09019000 P1 19 WS 8 TT 09012000 P1 19 WS 8";

		// 7E7E3C8888666601000035002302000D130104153646F1F1888866660150F2F279616E79753A59435A2D32412D3130312103BB20
		return cmd.send35Message(1, null, rgzs);
	}

	/**
	 * 测试发送图片报，测试发送通过
	 */
	public void send36Message2() {
		cmd.setFuncCode(new byte[] { (byte) 0x36 });
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
		String viewDate = sdf.format(new Date(System.currentTimeMillis()));

		String fileName = "c:\\bar.jpg";
		byte[] pics = readImage(fileName);

		int len = pics.length;
		int pos = 0;
		int amount = 0;
		int seq = 0;

		if (pics.length < 4071) {// 判断是否大于报文体的最大字节数
			// 如果小于，则做为单包发送
			cmd.setBodyStartBit(Symbol.STX);
			cmd.setEof(Symbol.ETX);
		} else {
			cmd.setBodyStartBit(Symbol.SYN);

			// 先计算出总数

			while (len > 0) {
				len = len - 4071;
				amount++;
			}

			len = pics.length;
			while (len > 0) {
				seq++;
				if (amount == seq) {
					cmd.setEof(Symbol.ETX);
				} else {
					cmd.setEof(Symbol.ETB);
				}

				byte[] pic;
				if (len < 4071) {
					pic = new byte[len];
				} else {
					pic = new byte[4071];
				}
				len = len - 4071;
				System.arraycopy(pics, pos, pic, 0, pic.length);
				pos = pos + pic.length;

				cmd.setBodyCount(HydroBuilder.buildBodyCount(amount, seq));

				IMessage message = cmd.send36Message(100, null, "0011110000",
						"P", viewDate, pic);
				session.write(message);

				System.out.println("count " + seq);

				printHexString(message);

			}
		}
	}

	public void send36Message() {
		try {
			String hexstr1 = "7E7E42000055566603E836041C1600A0010002130306185309F1F1000055566648F0F01303061853F3F3FFD8FFE000104A46494600010100000100010000FFDB0043000C08080A08080C0A0A0A0C0C0C0E101C121010101022181A141C28222A28262226262C303E342C2E3A2E2626364A363A40424646462A344C524C44503E444642FFDB0043010C0C0C100E1020121220422C262C4242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFC000110800F0014003012100021101031101FFDD00040002FFDA000C03010002110311003F00E1ECAD4BB024574563643038AECA3139EACAC6D5AE9E9F7B183EB5B36F6D8038AF4E94123CDAB3B9FFD0EB121005739760497B311D3762BE82B24DA4785877AB1163A6DDA6D87EA6B2A8AD4D9D517EF23FFFD1802D3C0AD4C45C52E290CFFFD26E2931566418A3140CFFD36628C55198629314867FFFD48F15281C0A640C907348A391401FFFD55C518A640B8A5C5311FFD67E2B2F571FBC8BE86896C4C3E228E2971581D27FFFD7C0C51B9874661F8D606D617CE947491BF3A517938FE3CFD6A93643823FFFD0C2FED09875DA697FB45BF8907E750A4C6E98C92FC6DF95483EF5524BB958E0607E14F998946C7FFFD1C0B0B2C6315D059DAB0C700D7A74A2CE0AB2362DA1C632AC3F0CD694088C3860474EB5DF0D11E74DDD9FFFD2ED4A610FB0AE502E6473EAE4FEB5EEB97348F0E82B5C9556A1BE1FBB51EF535DDA9B3A21F123FFD36014B8AD4C45C52E290CFFD431498AB330C518A00FFFD56E28C55198628C5033FFD66E2A503814C81920F9BF0A6AF51401FFD7752D3205029714C0FFD0971597AB8F9A2FC6896C17E2B4";
			byte[] message1 = ByteUtil.HexStringToBinary(hexstr1);
			session.write(message1);
			Thread.sleep(100);

			String hexstr2 = "7E7E42000055566603E836040D1600A0020002130306185309F3F3287C450A2B9CE83FFFD1C134D26B0B1B5C4CD21A62B9FFD2E6C8A691589A8C65A458C3B804645311FFD38AC6D781C56EDA5B8E2BDDA313C7AD2352084002ADA420E3201C74ADAACF956871DEECFFD4EEA489638243923E527939AE5618F0833D7BD7B945B9C9B678749D932611D54D406360FAD562B4A4CDE8BBCD1FFFD500A315AB31140A5C5219FFD67629315A19862931480FFFD731462A880C5262803FFFD04C54EA381F4A64114A3E6FC29AA3E6140CFFD17E29714C8145385303FFD29C8ACBD60731FE344B6143E233F14A4702B9CE967FFFD3C2C5262B9EE6E26293029DC2C7FFD4E78AD215AC4D5A18CB5358A8FB5C608C826989EC7FFFD5BF676D802B62DA0000AFA1A51B1E155917E28855B8E1CFB573622A18C51FFFD6EFB53023B099BD10D72F1AE147D2BD9C1DDA6CF120AC99262A8EA1F7D47B5698BBFB266D43E347FFD75C52E2B5320C52E2901FFFD07E28C568662628C5219FFFD15C518AA203146290CFFFD2502A70381F4A6490CDF7BF0A62FDE14303FFD39B14629922E28C5311FFD49F159BAC0FF55F5A25B0A1B99D8A53D05739D2CFFFD5C3C52639AE73A008A4C5007FFFD6C1C5262B036118714FB518B98FFDE14C47FFD7E82D61031C569C480638AFA46ED03E726EECB9020AB4A0638AF26BCAECBA6B53FFD0EFF576DBA6CE0FF1215FCEB9C55181F4AF6B2FF859E2C9598F0A2B3750C79E3FDDADB1CFF746987F8CFFD17E29715A990014B4867FFFD2969315A19862931480FFD37E292A880A2819FFD4515640E05324827FBFF85313EF0A00FFD59F14B8A648B8A4C5311FFFD6B38ACDD607CB1FD6896C286E668A763E515CC7533FFFD7C5C5262B98E9131462811FFFD0C2C5005739BB11871443C4C87FDA154893FFD1EB6DE2C0ABD1267A57BF59F2C6C7CE6ECB91A002A402BC89BBB3A68C4FFFD2EF35E1FF0012D7C0FE251FAD61018AF672F578C8F22AEE18ACCBD39B86F6AD31D754FE65E1FE33FFD39714B8AD4C83146290CFFFD49F1498AD0CC314845219FFD59292A880A4A433FFD670AB40702992579C7CE6991FDE140CFFD7B38A5C532431462981FFD0B58ACDD607C91FFBD44B6086E8CDC53BF82B98E967FFD1C63495CC748868A047FFD2C2665519278AAEF7B1AF439AC126CD9BB1224C92AF079A913EF2FD69DAC24EE7FFD3ED624C62AF42B8ED5ECE2A47CDC513814F15E5B3BE92D0FFD4EEF5E602C821FE275FE79AC6E2BDDCB95A9B7E678F5BE20C7159579CDCBD3CC1FEED7A9A617E33FFD59E8AD8C829715233FFD6B18A4AD0CC29314867FFD79A9B5448941A407FFFD078EB5680E05324AF3FFAC34C8FEF8A18CFFFD1B78A314C9171462988FFD2B86B3359FB91FF00BD44F6086E8CDA70FBB5CC7533FFD3C6A2B98EA108AAB7D39863C2F534E2AEC99688FFD4E20CAE41CB1C1A6679A1206C724A17D25E";
			byte[] message2 = ByteUtil.HexStringToBinary(hexstr2);
			session.write(message2);
			Thread.sleep(100);

			String hexstr3 = "7E7E42000055566603E836040D1600A0030002130306185309F3F3CBF749ABD6B75C80C79A52571A763FFFD5EF215AB89803A81F8D7A3887767CFC2D71C244FEFAFE74F521BEE907E95C928C9743BA9CE1DCFFD6EDFC460F956DD7994FF2359F6F079F26DDEA800C966380057B9836A34399F43C7ACEF3D0B7268F284F312685A3232A4B633FD3F5AE6AF11A3BA951C6195CA91EE2B9B11888D682B686F422E32D7B1FFFD7B1456C6485C518A919FFD0B341AB204A4A00FFD198D36A890A43480FFFD278EB57314C92ACFF00EB0D2463E714867FFFD3BB8A314EC48A0518A607FFD4BA6B33591FBB8FFDEA27F0843746653C0F93F1AE53A99FFFD5C7A315CA7501155AEAD8DC615464FF002AA8EE4CB63FFFD6E45B4C94872A385EB4CBAD3DEDA2490F2185172B94ACA06334F57029907FFFD789358F166AF916D1C888473E5C781F99E6ACAF847C557BCDC5E3A77F9E73FCB35DEE136AF52565FD743CE5EC693B25764C3E1B6A720CBEA7167D0963FD28FF008575ACC3930EA91E7FD97753FCAB26A9DFE27F71A2A8DFD8FC8FFFD08750F0FF0089B4BF2D5AEE59D49CAFEFCBE3F3355935AF10696D99622F8E3F791E47E98AEE5ED545B8BBC59C56A53D1AB3376C7E27AAC71C37360D115E0C9149FD0A9A85F508751B89A789D5849233E01CE32738AC5B8DBCCA852707BE87FFD1B14B8AD4C85C514867FFD2B5495A102521A407FFD398D255122521A433FFD4917AD5CC55224A93FF00AD6A22FBE2A467FFD5BD4B5448514C0FFFD6BA6B3B58FF00571FFBD44FE1087C48CBA78FB9F8D729D6CFFFD7C9C518AE53AC5440ED8270314B6B1152DC64A9FCEB486C672DCFFFD0AFE4AE64C0C2B8ACDD61316817FBB51D4DDAD0E7734AA7E6AD0C19FFD1F488D0228000007A0A900ADEA49C9DD9E45043852D64CEF89FFFD2ECF5F27ED76CBDB6B1FE5544AE4104641EA2BDFCBE29D0F99E4621FBE675EE81637CA77C0AAC7F893835C95DE8B75A7C8D2DA48CCAA7B75AE5C7D18C1A71EA74E1AA3774CFFFD3A1A66B625610DCFCB2740DEB5B239AD6E66D0B8A290CFFD4B74D3C1C1AD081290D219FFFD598D255122521A407FFD6917AD5EC5508A53FFAD6A21FF582901FFFD7BF8A2A89168A607FFFD0BC6B3758FF00571FFBD4E7F0B087C48CBA917EE7E35C875B3FFFD1C9A5AE53AC7A611896E3029F6ADB9D87426B582D0967FFD288B1DB8E954EF6112C6C0F71599BB395953648CBE869A3822B53067FFFD3F4B029C056B26799462C7014B59B3B123FFFD4ECFC40765D5B371F71BF98ACFF003857BB819A8D148F26B46F3B879A3158C7926B1C7CB9B97E66D868DAE7FFD5A3ABE8E93A34F0AE1C72CA3BD1A1EA664FF45989DEBF749EF5A75216C6D51401FFD6BF6EA0B938CED52715032062491935A7433EA376153C138F434EC13D8D21A3FFD79882382314954489494867FFD0953A8ABB542295C11E73722960FF0058176B7C";
			byte[] message3 = ByteUtil.HexStringToBinary(hexstr3);
			session.write(message3);
			Thread.sleep(100);

			String hexstr4 = "7E7E42000055566603E836040D1600A0040002130306185309F3F32901FFD1D0A2A841462988FFD2BE6B37581FBB8FEB44FE1610F89195DEA403F767EB5CA75B3FFFD3C9AB96700705FAB0E82B9A2AECEB665CF74F1CD89396DDCFD2ACDBDD441B2A464D6D621C95EC7FFFD44402540C3A1A86EA3FDD9FA566CDD1C65C8226707AE6A2CF22B5307B9FFFD5F4A56CFF000B63D6A406B4923CFA3256141A5CD458EA4D1FFFD6EB3C4D27FA5C2BE919FE7593E657A9879DA9A470495D8799C553ACF132E6B1AD256B9FFFD7B75CE6B36A6C6ED2EA11804E7E86B56668DFB59D6E6DD255E8C3352D219FFFD0D5B3898F984A90BE5B60E3DAA9D6AF6467D463B6D527B019ACCD3EE6E6E6295CC84AEFF9477C54147FFFD182586E66871148C8D9EB8CD5617F776442DEC5B93A79A94D08D18A549903C6C194F42294D303FFD2A575ABAC2FE5C03CC93A714AB6FAC5E7CD230841EC4E31F855211149A5DE46E47DA158FAE4D3546A56AC1972F8F4E6803FFFD3AD6FE2050DB2EA3287D6B5E2952650D1B0607D2A843E8A623FFFD4BE6B3759FF00571FD689FC2C21F1232AA55FF547EB5CA75B3FFFD5C696648465D80AB1A35E8B89DD101017D6B082EA74CB6296A815EFE4703291F078EF510F29864607A115B10EDD4FFFD6C8D33535B72D1CCE02F626997FE228CE52DD0B7FB4DC0A971BB2F9AC73F33B4AE5DB1927B5458AB2373FFFD7EE22D589387C039C6C31B03D71DCFD6A7FED8849C2C4EDEA432E07EB5E84F0D1B5D3B7A9E3C6A4A1A344C97F111966451F524FF2A53A8DA860A1D89F68DB1F9E31594B09556C8E88E223D51FFFD0EA35F8E7BBBE568219644110E550919C9F4FC2B21D2488E24464FF007862BBA319A56B1C50945A1A1F70E0E7E94B0DA5C5C7FAA82593FDD426A27791AC6C8FFFD1DA8F41D4A4FBB6AE3FDE217F99AC3F14DBB5841F67BC4F2E561B906E07F91AD9AF3324CC9D1356F282DA796F2166F976F6AE9C4127FCF36E3DAA516CFFD2EA5109F0C1C8C1121EA31DEB05C1C8E2B4666B72BDCF30498FEE9FE554BC3CCA2C412A1BE623045228FFD39A69493900041FC23B531D43A9560083C106A8932581D1EED483FE8931C119FB869FAA5EBE56D6DF991F8E290CFFD4874CD3A3B300901E53F7988FE55B554228DC7FAE6FF3DA8B7FF58290CFFFD5B175A7DBDE291220CFF780E6B12482EF4397CC889783B8AA11B5617F15F46190F3DC7A55AA6847FFD6B467DD3989173B46598F41EDF5AA97F19BA1F29C2A2963C7EB5A2839E888E751D599924421DC1CF3DA9924CB05B33B1EFC7BD73CE938E8CE985452D8FFD7E32E2E1A772CDC7A0F4AD1D075186D4C8B20019B90D52A3A58D252D4EA6CFC1326A162933DDA209B321001279AB56BF0EEDA31892EE46FA2D6F1A6BA98BA8DEC7FFFD0D59BE1F69CDF379D2834D5F87FA5A725A47FAF15BF2C4C9CD91DD7C3FD3A688F92CD1B76AE5B50F03E17E8EE";
			byte[] message4 = ByteUtil.HexStringToBinary(hexstr4);
			session.write(message4);
			Thread.sleep(100);

			String hexstr5 = "7E7E42000055566603E836040D1600A0050002130306185309F3F3A36926224F394F42289413D8233EE7FFD1D1B4D4A2889790CAEE49E4228EE4E3031DC9FE5D000241A94050A66EC29C9DB95C57A32A94EA45295F43CD74249DD16A3D4AD7793E648031070C090BF4A94EAB68327ED07A60623271FA5744ABD392B27623D8CD743FFFD2E9E4D6F4F8220EDE6CBD8AC4A7731F5E7007FF005AAB5E78C2C6CE21247A7B4BB48FF5B3AC7F9E4E2BD1AD5E57B45E87053A2AD79233A7F88333CB08B6B0B0405BAB9F308FA14CD3EE3C53AE4A1A559D5157AA430649FA6EAE5F8B56CE8B28EC7FFFD35975DBF9D9646D55D242F810C93F9442E325B8C8C678F5F6EF56B5748EFBC182F273E64EB740091B32301B7EEEEF4E73E9F8D6DB2FEBB99ADCE2AC5FC9BF89909077819F4AF5383C2D7840325F4583CFC884FF005A9565AB29AB9FFFD4EB6FF76996234F6B812EF39DC5304739F5AC795761C249FA569722C65EA7A89B34649D14ABA90AC003597A1EAD6D6F018245DCDBC91C76A4C11FFFD570D4EC1BAE57F3A12E2CF69266C2E7E539AB2752AEAB2D83DA14698B0707054670474EF59DA1F952CF24AD229907CA8A4F38F5A433FFD69A2FBE3EB578D5213285C7FAE6A5B7FF005A290CFFD7D1A6BA2C8A55C020F506AC939EBFB29747B8FB5DA93E567E65F4ABF1EBF6CF0AB7CC5C8E540A5B0ED73FFFD0A171AFADB19196025A4E704F4AA29E20998B67CB5054AE0A93570AAE3B04E95F7207BE9676DA590E7FD922AADF5CF98C1036554763C135336E4D5C74D28A67FFD1E109CD3E2E096F414C1BB9ECDE1A904FA159329CEE8C66B576815B27A2313FFFD2EF6539E2A3439E2BA4C191F98A1F00F7C51248A0E01E68B89A3FFFD36AD480D6E60381A70340CFFFD4926E50FCDB7DF8FEB5048A658CC6E4EDC8F991B9FF00D0715BEE62655DB2DA5CC6915D4E0919D9334CCADED803152B87953CC4B4277E0318E0C823AFF130EE295EDA0FCCFFD5CC663103991D700677470A63F1E6B76EA413780E36DC92017630DF68F371C1E7E5C05FA7D4F715A74641C448CD0DC6F493E656C865AF6BF0DEA89ABE8D6F72ADB9CA0127B30EA293EA3E87FFD6DEF16C862BD524F45CD72171AEDC48C44611547009E6AFA90B6295C5CCD78009A50C01C818E95125BA29C89307D851619FFFD7C11096E9707F114E36F38185991875C569CA4DC8A44B851F3C591EA2A1B4BA4B1B8F31539C608352D5868FFFD0A36DAFDB9653202BCD6DDBDEC176330C81BD8552115A7FF5CDF5A75B7FAC14867FFFD1D1348CC15493D866A8939C9EE5EE9E42ECDB4F1B73C01F4AC960D6B2EF03E5CD65177DCE99C1452B1FFFD2E56E1C4ACA4739A819503743906A15CE8934F51EEA4A194F1FDDAA4C79AD4E647FFFD3E0AA43F2A05EE7934C0F56F87F7A975A247183F34276B027A56FCF7218ED4C91EA2B44D58CEDA9FFD4ECE47918E14607A9AA97175738";
			byte[] message5 = ByteUtil.HexStringToBinary(hexstr5);
			session.write(message5);
			Thread.sleep(100);

			String hexstr6 = "7E7E42000055566603E836040D1600A0060002130306185309F3F36F73020F2177B1E0FB55B93E84C62AFA9424B5BE982E1CC63A9C9A5135DD9FDEFDE29E0D4C5BBDD973E56AC8FFD5629A9056E6028A70A067FFD67CC014E49FC33FD2B39E3569DB6B5D67FEDB95FD0815B98956F84BE7A0FDDC881394649B3DFD0FD3AFA50D663C95DBA767907096F9C71FED354F52BA1FFFD7C5304E1815B7BE8F0070B6D011FA9A9AF7569AEECA3D2F79F2226DFCC4A096E99F9475C1C569733305F86239AD9F0AF88EEB42D413CA6CC12B859A32782338CFB11FE7B524EC56E7FFD0BBE33BE8E49B746DB8797C1F5AE255AB47A4888EC3D5853837BD033FFFD1E70360D481FDEB4247ACC7D6B2EE4E676FAD12047FFFD2E2F26B5B4295A3D421C13863834C0E8663FBD7FAD496DFEB07D28EA23FFFD3D135535397CAB3908EADF2D53D98455E48C3847F163AF1515C8593218715CFD4F42DA1FFD4E3CFEEA4C1E80F14E6DAC700E7D69799A3D3DD1D295F271DF159EDD6A8C91FFFD5E0C7240ADFF0968CBADEB1E548BBA341923D6989E87A569FA2C7A6AB2DB46B186EBB7BD5916D213F339A770B1FFFD6EE0DB37A8A8DE265FE214D80D0A8C70586691ED51B3F30A13B89AB1FFFD78D6A415B988E14A0D203FFD09BECD3DCA110DBC9311D762E76FBD42DA5DCEE3E65AB01E8D003FD6BA145B306EC65EA1A4B4B70A0E991CA150004C0DC75F460285B07112A0D3982F0194419038FAD274E4352563FFFD1C3BFB478A2693EC663008059A10BD87D6B3DE6675C9EBF4AD1A69EA4269958B1CD465D81E09A928FFFD2E4ADE791A270CECC02E002734DDC7DEA90850E7DE9C25C7AD303FFD3E57CD1EB4E12FBD5923BCDCE39154A76FDEB73431A3FFFD4E2770AB5692E278C898C241E1C0CEDF7A606BA6AB3A3379816E547F1C479FAE2B4F4ED42DAE24C2480363EEB707F2A047FFFD59EF6FE2B34CB6598FDD45EA6B066BF9EF269165CA84380838039EFEF44B62E92F7AE4B811C03D6A8CCFC135823B7647FFFD6E3D8F9A481D077A683B1B0692EC6927AF30F6FBB51ADB23160F32A63F5ABB191FFD7E3923822DC7CD0ED8C2E2BADF86A3C8D42E25DEA7298001AA4B513D8ED350D53EC70B3052CC06719FE66B286AF757647CC22F9431C0CF7E467FF00ADDC7AD2651FFFD0EC2DA7964F95D81231B88E467BD4B39C46CC3D2981C3B6A37AF2CC7ED2C02B90053E2D4EF4C418DC1CE718C5652DF43A69C135A9FFD15821593AB853EF530B65C9FDE0E2BA94158E573698E16C9DE514D96358C0C386CD0E292D071936F63FFFD2B56B70F6D2ABAB3A8C8DC1588DC3D38ADB4D5AC4659A6B81F5790FF5AEA8C92460D5CB115DB6A31AB585DCAAA07CC583FE1C9A93FE268993F6CDE7B03C7FECB438A62BB47FFFD3DCD7AC755D5B4E7B696657DCE0AA63818EF9C67FFD55C8BF83F554054401BDC1ADE51D74318CAC567F086AC39FB371FE17F372";
			byte[] message6 = ByteUtil.HexStringToBinary(hexstr6);
			session.write(message6);
			Thread.sleep(100);

			String hexstr7 = "7E7E42000055566603E836040D1600A0070002130306185309F3F3F0AACDE19D414FCF09159F2B2F991FFFD4E79345B98548752A48EE2A0934EBB8FA2AB8F546DC3F4AAD49BA21686741F346C3F0A84C873D2819FFD5E337B1EC69CA243D11BF2AA10EDB2FFCF36FCAA3686566CF96DF95007FFFD6E33EC933748DBF2A963B4983AA15DA4F40475A622DA693760AE42273C12454B728D1C8A6EE3132F66420367D6981FFD7E756ECDA3B4F6B233E1B0AB2A16E3EBFFEAA7BEAC970416B6119C7CC51473EE69BDAC5C1D9DC26D4629460647D455096E3CD3B41C28EFEB59C636369D44D591FFFD0E41594702875056A0DF4688F715F94F4ED4C3210E095538ED8EB5A1CE7FFD1E30CD1B2E3C88C13DC66BB1F87E63F3656588211DF7673548967597D6497B9DDC7181EDFE7FCF4A7DBD8C102E1517F1029147FFFD2ED1555400000074C532762B13F71B4D303864B78DBCD7DE77166CFE66922B62421C9CE4719F7AC65B9DD0F84FFD3B33446523620423B81C7E548B6F30E847E55D9677D0E24D5B517C89FA8603E829C96D292774EE07A79719FD4A934D5D306D1FFD4BC21913FE5EA43EE628BFF0088A61597A3CDE6AE41DAD1A01F8ED033F43C576B9497538D24CDED22676B6DD26D0777F08C0AD0121E79A1BBEACB4AC7FFD5EE3CD22944E7B807F0AEB68C0912EC2E73121CFB5325B9561C4282A39751DCFFD6EC2595083FBB15CD6AA54CBF280BF4ADE5B192DCCC9137020D67CB631EECEDAC8B3FFFD7C55B68D4FDC156A201460014C45A4638152019EC3F2A047FFFD0539DB8538AAF75602ED36B9C11F7580E4532519D31BBB3016E15A541C2C8BCE067BD2EFB69B2CD7508079C32EDFEB40CFFD1E782471DC3013284CE723A669862F370B6CAEEC3F882E0114DAB8D3B0F7D1AE5D41675CFF76A23A35C0ECBF9D2B0F9BB9FFFD2E67FB1E7F6FCE8FEC9B91D0AFE268B1775D0866D3EE13EF46481DD79AAEF195E304FE14CCCFFD3E323B5926CF97838AECBC0D09B70E58E19BA8F4AA44B3B10E0F7A76F1EB48A3FFFD4EC838A46C3A953DE9819E347B70F9D8B8272719F5A63E8F16ECA2818F7349C532954923FFFD5B697519FE25FCEA41770FF00CF45FCEBBB991C1CAC437D001CCA83F1A63EA7688096B88947A96149CD0D419FFFD679D6F4F2C545EDB93E8241519D66CB271306FF00779FE55D2E68E78C59B5A66B9651D9297936F2782307F2EB52BF89EC41F97CC61ECB8A1D4487CACFFFD7D3BBF148C0F262381D771C1C55BB4D556591403956C30FA56EA776656B23555E324EE2DD3B535997B1AB649FFFD0EB25C6D3CD739A8E04879CD6F2D8C56E51623D6ABC983591A1FFD1CDEFD29EA48A622542DDC8A90301D5BF5A00FFD28D66407E539FC69DF683E829DC5633EF355D87CA8977B9E302AB43A54939F3266099E70A3145C67FFFD3A51E9D6F18188C311DC8CD5858F68C2F03D00A621769F7A4F2C517BE88";
			byte[] message7 = ByteUtil.HexStringToBinary(hexstr7);
			session.write(message7);
			Thread.sleep(100);

			String hexstr8 = "7E7E42000055566603E836040D1600A0080002130306185309F3F322AE7FFFD48BCB14BE52D3420F297D0521B7898728A7F0A623FFD5C9BBD1BE6F32D4956FEED2E9FAE5C693211343CF4CD513B9BD6DE256BA19444FA6E15706AB718FF519FA11536655D1FFD6D6FED59C7FCBBB5386B12F781FF2A2CC574386B4C3AC0FF951FDB63BC4E3F0A7A8B43FFFD7E5068B37FCF54FD69FFD83391C4A87DB0DFE15A7299F300F0FDD1E84548BE1ABB3D59451CAC39D1FFFD0C2B7D06281F75D4BBF1FC0BDEAD2C783B608828F45156D74213B93C7632120CAC221EA79AD1B4D334D9572DA8027D00AA8C175139763FFD1BB73A2D83444412B3363A9615560824B3915986154FAD6CE16D519293D99D05BEA114AA32573564488DD36D697B92D58FFD2EA25DA50E315CEDFE04A79ADE7B194772849222F522AACB7512F561591691FFFD3C437C9FC20B7D0520BB94FDD8F1F534C4384970FFC4ABF419A72C0CDF7A476FC714867FFD4CF8A2D830AA6A1BFBB68144683F78DC014580769F60D10F3641990F393DAAF856A2C07FFD58F0D461BDE8B0061E8DAFE945981FFD66047A508D4EC2628434E0B8A7611FFD750295ADE29D4AC91861EE2AEC419F71A026E2F0398CD4206A766701D9D47E34AC3B9FFD0CD1AFDDC270F0E7F4A77FC250D8E606CD51161ADE2691BEE40D9A85B54D46EB88E3DB9FC681E88FFD148E15F41530445193815D36391B2196FA0841C104FA0AA536A92483098515129F4469185F567FFD2A16D04970727207726B4E28D211851F8D6B146327D09D6418E714C91A16043A230F719AA6C48FFD3475831848F6FFBA71FCA99E46F180CE07FBE4FF3AD1B331534D71C8B8957F1AB296F749F76EDFF00114243B9FFD4B045F6306EB3FF0000FF00EBD529ECEE256F9AE78F65E7F9D68C8440747427E795DBEA6946936EBFC34AC3B9FFD5AE34F847F0D3859443A20ABD091E2D907F0814E1081D00A047FFD66CBB6089A46380A33595A5DB9BEBA7BA906541F941AB64A37027B52841E9401FFFD79F68F414BB47B551221E3D29A585007FFFD07E4D2E09AA2436D2EDA623FFD19714F515640EDB9A431834C47FFFD299ADE36EAA0FD4542F6301FF00966BF95686447F63897A22FE54A14274E052B05CFFD3824BE8E11D727D0567CF7F24C48CE07A0AD672E88C211EAC8E2825B8385535A56FA5AC78694EE3E94A11BEA54A56D0FFD4712106178151B4D8EF5D0CE75A8D3293DE85CB1ACD9A23FFD5544A9D140AD52332C2529A623FFFD6BE4D44DCD6ACCC691498A433FFD79B6D1B6B42036D18C5203FFFD0CED7262DE5DB21E5CF35A3656EB6D6E8838E39AA249F7A8EF47983B0A607FFD1B1963DA8DA4F5354486C146D03B51603FFD2B18A4AB204C51401FFD39A9EB5640EA334C47FFFD4B59A6B5686444D514CC1518FB5007FFFD5E6D0BCCD81926B4ED34D030D2FE55718DCCE52B234902C6B85000A47941768B7";
			byte[] message8 = ByteUtil.HexStringToBinary(hexstr8);
			session.write(message8);
			Thread.sleep(100);

			String hexstr9 = "7E7E42000055566603E836040D1600A0090002130306185309F3F301D6B6BD918EECFFD6ACF364F5A68CB568D992562544A9D12803FFD7982D3C715A991206C52E68B81FFFD0BD9A8CD6A662525203FFD1B18A2AC80C53483401FFD2CED56DE532ACC8B92B5546AF7319C4887154C94588B5C847DF539F715761D66D5FF880A131D8FFD3923BC85FA38A9432B7420D5922D1401FFFD4B348455902628C5007FFD59E954D5903B3484D023FFFD6B04D349AD0C88C9AA5A8CDE5C279A016E7FFD7A16B0C70018EBEB567CF51DEBA1592399EAC8DEEBB0A84BBC86A252B97147FFFD0A91C24F5A9D62C56A919364AA98A917814C47FFFD19C1A5CD6C642834E06901FFFD2B64D21AD4CC4A4A407FFD3B3456840518A407FFFD4B0541EA2A092CE193EF20AD0CCAB268D03745C55597415FE134AC3B9FFD5C27D26E22E6366FC0D304B7F6C7EF31C7AD5589B9660D7A48CE2543F5AD2B7D620987DE00D0163FFD6996647E55853B35640525007FFD79E945599866909A607FFD0989A6135A190C63589AB5C6E6D80D2635B9FFFD1C51396E94F5DCC6B4BDCCAC4F1C59EB56162029A426CFFD26A8C5381AD8C07034A0D007FFFD3941A5CD6A642834E1401FFD4B59A4AD0CC28A00FFFD5B34568662D25033FFFD6B3455902E290AE6803FFD7B06307B544F6EAC3040AB3329CFA545267E5C566CFA3489CA7E9458773FFD0E704979687A9207AD59835E74E2453542DCBD16B90BF561532EA90B771F9D17158FFD18C6A109FE214A2FE1FEF0AB22C2FDBE1FEF0A437F17F78517158FFD284EA108FE21513EA508FE2157733B15E5D5632A42B0AC6B898CAE5A93652563FFFD3E762990F715762953D6B4466CB28EBEB53AB0AA20FFFD46034E06B639C5069435051FFD5766941C56A64381A703401FFD6B39A4AD0C85A2819FFD7B34568662D1401FFD0B545686614629203FFD1B74568662114C2A0D007FFD274B6B1C839506A84FA344FD062B43328C9A1B29F96A16D2E55E80D2B0EE7FFD3E69AC271D8D30D95C7AB7E7544DC6FD8EE7D5BF3A436773FDE6FCE80B9FFD4E4FEC339EA5BF3A6BD8C8A32C4FE7544DC882F974E405DB14867FFD5E115D87426A54BB953A3555C92C47AA48BD79AB516B38EBC534C5CA7FFD6C68B5746EAC2AD47A846DDC56B732E52C25CA377A915D4F4340AC7FFD701A506B4321C0D381A00FFD09F3466B4320CD1D690CFFFD1B14B5A198668CD007FFFD2B34B9AB330CD2D007FFFD3B745686621A4A407FFD4B148455998D20534A0F4A623FFD50C6BE94C312FA0AD0C869897D29A625F4A00FFD6A13BA460F4AC7BBBCDC485AB6424512493566DD31CD48CFFD7F3FC38ED4A1CF714C4287A70614C0FFFD0E133E94E595D7A31AA11325F4C9FC59AB316AF22F5CD1711FFD1C18B5B1DCD5C8B5746EE2AEE4729652FE36EF532DC237F10A2E2B1FFD255914F4614EDC2B4321DBA9770A067FFD39C1A17AF19";
			byte[] message9 = ByteUtil.HexStringToBinary(hexstr9);
			session.write(message9);
			Thread.sleep(100);

			String hexstr10 = "7E7E42000055566603E836014C1600A00A0002130306185309F3F3335A198B9A2803FFD4B14B5A1986696803FFD5B549566619A334C0FFD6B048A4AD0C84269B9A00FFD77134D240AD0C8864B8441C9159F75AA2A020114AE348FFD0E4AE2F9A53D702AA96CD3112C51E4D5E86227B504B3FFFD1E54411B0E4506C236E955622E46FA59ED503E9F22F4069582E7FFFD2E15ADA55ED4C2AEBD41A6486EF5A5DC2819FFFD3E1334A188E869887ADC48BD1CD4C9A8CCBDF3401FFD4E5A3D6245EB9AB31EB9EA71542B16E3D654FF10AB09AA46DDC517158FFD5856FE33DC54AB7519EE2AEE4587ACC87B8A70917D6811FFFD69830F5A5C8AB330CD2E6803FFFD79F34B9AB33133466811FFFD094B0F5A6991477AB3218D3A2F522A27BD8D7B8A0763FFFD1A136AA89DC0AA171AC8E769CD55C9B19D36A124B9E71559A427A9A433FFFD2E179353C30963D299268DBDAFA8ABD1C41474A6910D9FFD903BD2E";
			byte[] message10 = ByteUtil.HexStringToBinary(hexstr10);
			session.write(message10);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void send37Message() {
		String hexString = "7E7EFF0000000003FFFF37002A020015130503143621F1F1000000000348F0F0130503143620190000002619000030391A0000123812126103C5AE";
		hexString = "7E7E010011420402000A37002502002B140915133356F1F1001142040248F0F01409151333201900000026190038143812137603B9E3b";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send38Message() {
		// String hexString =
		// "7E7EFF0000000003FFFF38001F020016130503144021F1F1000000000348F0F002010605000418000100F46000030085";
		String hexString = "7E7EFF0000000003FFFF38001F0200A0130505152450F1F1000000000348F0F002010605000418000005F46000036EBE";

		// hexString =
		// "7E7E30556677889900003800DE02000A121116164136F1F1556677889950F0F012111600000418000000F4F400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001E000000000000000000000000000000000000000014000000000000000000173E1A";

		hexString = "7e7e10002345678912343800de02003d151224151442f1f1002345678948f0f015122315000418000000f4600000000000000000000000000000000000000000000000000a280a001900000f05050519140a140000001919000a0000000000000000000019000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000170649";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);

		// hexString =
		// "7E10020E3F1506240900000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1220FFFFFFFF02600281FF0000033FC4";
		hexString = "7e7e100023456789123438006202003e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000375f3c00000000000a000000002300000000001e00000000000000000000000000000000000000141e1e000019000023230019033425";

		byte[] message2 = ByteUtil.HexStringToBinary(hexString);
		session.write(message2);

		// 7e7e10002345678912343800de02003d151224151442f1f1002345678948f0f015122315000418000000f4600000000000000000000000000000000000000000000000000a280a001900000f05050519140a140000001919000a0000000000000000000019000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000170649
		// 7e7e100023456789123438006202003e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000375f3c00000000000a000000002300000000001e00000000000000000000000000000000000000141e1e000019000023230019033425

	}

	public void send39Message() {
		String hexString = "7E7EFF0000000003FFFF39000A020017130503144321F2F2034621";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send3AMessage() {

		// String hexString =
		// "7E7EFF0000000003FFFF3A001C020018130503144521F1F1000000000348F0F013050308001F190000000390D7";
		String hexString = "7E7EFF0012345678000A3A0046020011130306152635F1F1001234567848F0F01303061400F4600A141E28323C46505A646E78F0F01303061400F5C00064006E00780082008C009600AA00B400BE00C800D200DC03736C";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send40Message() {
		// String hexString =
		// "7E7EFF000000000303E8400051020012130306152917F1F1000000000301204200000002280000555666031003E80450021921631230120050090538010131123456780C08030D4080010000000000000F683130313330313233343536373803C476";
		String hexString = "7E7E42000055566603E8400051020012130306152917F1F1001234567801204200000002280000555666031003E80450021921631230120050090538010131123456780C08030D4080010000000000000F683130313330313233343536373803C476";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send41Message() {
		String hexString = "7E7EFF0000000003FFFF41001C02001B130503145821F1F100000000030120FFFFFFFF0310FFFF0508000334A1";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send42Message() {
		// String hexString =
		// "7E7EFF0000000003FFFF42001502002D130503164024F1F1000000000321080522080803C813";
		String hexString = "7E7E42000055566603E842003F020003130306161220F1F1000055566620080121080222080823100060240805250910260910270802282300005000301B00400038120300401202004112010003AA74";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send43Message() {
		// String hexString =
		// "7E7E42000000000303E8430033020002130306155026F1F1000000000320080121080222080323100030240805250910260910270802282300001000301B002000039966";
		String hexString = "7E7E42000055566603E843001D020003130306155046F1F10000555666382216777215401200104112001003CA61";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send44Message() {
		String hexString = "7E7E42000055566603E8440035020004130306155154F1F1000055566648F0F01303061551701965535071196553507219655350731965535074196553507519655350035E8A";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send45Message() {
		String hexString = "7E7E42000055566603E8450024020004130306162124F1F10000555666145A5352373132302041332E36302048455820423103967A";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send46Message() {
		String hexString = "7E7E42000000000303E8460015020005130306162140F1F100000000034520000004A50349FC";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send47Message() {
		String hexString = "7E7EFF0000000003FFFF47000F020026130503161057F1F1000000000303C1EF";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send48Message() {
		String hexString = "7E7EFF0000000003FFFF48000F020027130503161100F1F1000000000303C9F1";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send49Message() {
		String hexString = "7E7EFF0000000003FFFF490013020036130503170548F1F100000000030310000A03CDED";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	/**
	 * 测试通过
	 */
	public void send4AMessage() {
		String hexString = "7E7EFF0000000003FFFF4A000F020032130503165848F1F1000000000303DCD9";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send4BMessage() {
		String hexString = "7E7E42000000000303E84B0015020006130306162243F1F100000000034520000006A503F5BA";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send4CMessage() {
		String hexString = "7E7E42000000000303E84C0011020008130306162350F1F10000000003010103AC1E";

		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send4DMessage() {
		String hexString = "7E7E42000000000303E84D0011020009130306162436F1F10000000003010103B28A";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send4EMessage() {
		String hexString = "7E7E42000000000303E84E001302000C130306162547F1F100000000030101000503DE62";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send4FMessage() {
		String hexString = "7E7E42000000000303E84F001002000D130306162602F1F10000000003FF03A52D";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send50Message() {
		String hexString = "7E7EFF0000000003000A50004F020012130306150821F1F1000000000300000004000000040000000000000000000000000000000000000000003D001300550000000000000000000000000000000000000000000000000000000000000313A0";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void send51Message() {
		String hexString = "7E7EFF0000000003FFFF51000F020020130503152852F1F10000000003033792";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public void sendC0Message() {
		String hexString = "681A68A33201010001C0260100000000000000000100530002401122006D16";
		byte[] message = ByteUtil.HexStringToBinary(hexString);
		session.write(message);
	}

	public String toHexString(IMessageHeader header) {
		StringBuffer sb = new StringBuffer(header.getLength());
		sb.append(ByteUtil.toHexString(header.getStartBit()));
		if (Symbol.UP == this.upDown) {
			sb.append(ByteUtil.toHexString(header.getCenterAddr()));
			sb.append(ByteUtil.toHexString(header.getStationAddr()));
		} else {
			sb.append(ByteUtil.toHexString(header.getStationAddr()));
			sb.append(ByteUtil.toHexString(header.getCenterAddr()));
		}
		sb.append(ByteUtil.toHexString(header.getPassword()));
		sb.append(ByteUtil.toHexString(header.getFuncCode()));
		sb.append(ByteUtil.toHexString(header.getBodySize()));
		sb.append(ByteUtil.toHexString(header.getBodyStartBit()));
		if (header.getBodyStartBit()[0] == Symbol.SYN) {
			sb.append(ByteUtil.toHexString(header.getBodyCount()));
		}
		return sb.toString();
	}

	public void printHexString(IMessage message) {
		String hexStr = toHexString(message.getHeader());
		System.out.print("MESSAGE >> " + hexStr.toUpperCase());
		System.out.print(""
				+ ByteUtil.toHexString(message.getBody().getContent())
						.toUpperCase());
		System.out.print(""
				+ ByteUtil.toHexString(message.getEOF()).toUpperCase());
		System.out.print(""
				+ ByteUtil.toHexString(message.getCRC()).toUpperCase());
		System.out.println("");

	}

	private byte[] readImage(String fileName) {

		InputStream is = null;
		ByteArrayOutputStream out = new ByteArrayOutputStream();

		try {
			is = new FileInputStream(fileName);// pathStr 文件路径
			byte[] b = new byte[4096];
			int n;
			while ((n = is.read(b)) != -1) {
				out.write(b, 0, n);

			}// end while
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		return out.toByteArray();

	}

	/**
	 * The main method : instanciates a client, and send N messages. We sleep
	 * between each K messages sent, to avoid the server saturation.
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		// 将数据长度与前四位整合
		// String hexString =
		// "7E 7E AA AA AA AA AA AA 12 34 33 00 57 02 07 CC 16 09 05 15 10 02 F1 F1 AA AA AA AA AA 48 F0 F0 16 09 05 15 10 F4 60 00 32 00 00 00 00 00 00 00 00 00 00 F5 C0 0A FD 0A FD FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 26 19 00 00 50 20 19 00 00 50 1A 19 00 00 50 39 1A 00 28 13 38 12 11 40 03 7E 9B";
		// hexString = StringUtils.replace(hexString, " ", "");
		// System.out.println(">> hex string " + hexString);

		// int plen = args.length;
		// int mPort = 50170;
		// String mFile = "";
		//
		// if (plen == 1) {
		// mPort = Integer.parseInt(args[0]);
		// }
		//
		// if (plen == 2) {
		// mPort = Integer.parseInt(args[0]);
		// mFile = args[1];
		// }
		//
		// System.out.println(">> mPort " + mPort);
		// System.out.println(">> mFile " + mFile);
		//
		// // 燕禹固态报文，此处测试无问题
		// if (StringUtils.isBlank(mFile)) {
		// mFile = "D:/workspace/RcvMsgGenerator/bin/msgdata.txt";
		// }
		//
		// while (true) {
		// List<String> lines;
		// int i = 1;
		// try {
		// lines = FileUtils.readLines(new File(mFile), "UTF-8");
		//
		// YfClient client = new YfClient(mPort);
		// for (String line : lines) {
		//
		// line = StringUtils.replace(line, " ", "");
		// if (StringUtils.isNotBlank(line)) {
		// System.out.println(">> line " + i + " " + line);
		// client.sendMessage(line);
		//
		// Thread.sleep(100);
		// i++;
		// }
		// }
		//
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// System.out.println(">>本次报文发送完成！");
		// try {
		// Thread.sleep(5 * 60 * 1000 );
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// }
		// ----------------------------------------------------------
		for (int i = 1; i <= 1; i++) {
			RtuClient client = new RtuClient(5017);
			Thread thread = new Thread(client);
			thread.start();
		}
		
		System.out.println("OOOOOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKK");

		// ----------------------------------------------------------
		// HydroClient client = new HydroClient();
		// InputStreamReader is = new InputStreamReader(System.in);
		// BufferedReader br = new BufferedReader(is);
		// try {
		// String cmd = "";
		//
		// boolean flag = true;
		// System.out.print("Hydro Client>>");
		// while (flag) {
		// cmd = br.readLine();
		// if (cmd.equalsIgnoreCase("exit")
		// || cmd.equalsIgnoreCase("quit")) {
		// client.stop();
		// flag = false;
		// try {
		// Thread.sleep(2000);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// System.exit(0);
		//
		// } else if (cmd.equalsIgnoreCase("help")) {
		// HydroHelp.printHelp();
		// } else {
		// client.sendCommand(cmd);
		// }
		//
		// System.out.print("Hydro Client>>");
		// }
		//
		// } catch (IOException e) {
		// System.out.println("系统错误！");
		// e.printStackTrace();
		// } finally {
		// try {
		// is.close();
		// br.close();
		// } catch (IOException e) {
		// System.out.println("关闭流发生错误！");
		// e.printStackTrace();
		// }
		// }
	}
}
